package com.soft.sitegenerator.ftp;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.io.File;
import java.util.ArrayList;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

final class DropFilesHandler extends DropTargetAdapter {
	private final DefaultTreeModel model;

	private final DefaultMutableTreeNode rootNode;

	private UploadFrame applet;

	/**
	 * @param applet
	 */
	DropFilesHandler(UploadFrame applet, DefaultTreeModel model,
			DefaultMutableTreeNode rootNode) {
		this.applet = applet;
		this.model = model;
		this.rootNode = rootNode;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.dnd.DropTarget#drop(java.awt.dnd.DropTargetDropEvent)
	 */
	public synchronized void drop(DropTargetDropEvent evt) {
		try {
			Transferable tr = evt.getTransferable();
			DataFlavor[] flavors = tr.getTransferDataFlavors();
			for (int i = 0; i < flavors.length; i++) {
				if (flavors[i].isFlavorJavaFileListType()) {
					evt.acceptDrop(DnDConstants.ACTION_COPY);
					java.util.List list2 = (java.util.List) tr
							.getTransferData(flavors[i]);
					ArrayList filenames = new ArrayList();
					for (int j = 0; j < list2.size(); j++) {
						String path = list2.get(j).toString();
						filenames.add(path);
					}

					System.out.println(filenames.toString());

					// Files or Folder added:
					if (filenames.size() > 0) {
						this.addFiles(filenames);
						evt.dropComplete(true);
						return;
					}
				}
			}
			evt.rejectDrop();

		} catch (Exception e) {
			e.printStackTrace();
			evt.rejectDrop();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.dnd.DropTarget#dragEnter(java.awt.dnd.DropTargetDragEvent)
	 */
	public synchronized void dragEnter(DropTargetDragEvent arg0) {
		this.applet.jContentPane.requestFocusInWindow();
	}

	protected void addFiles(ArrayList filenames) {
		int x = 0;
		File file = new File(filenames.get(x).toString());
		addDir(file, filenames, x, rootNode, filenames.get(x).toString());
	}

	protected void addDir(File file, ArrayList filenames, int x,
			DefaultMutableTreeNode rootNode, String filename) {
		if (file.isDirectory()) {
			// Show all the files in the directory
			File folder = new File(filename);
			DefaultMutableTreeNode folderNode = new DefaultMutableTreeNode(
					new TreeElement(folder.getName(), folder));
			model
					.insertNodeInto(folderNode, rootNode, rootNode
							.getChildCount());
			File[] files = file.listFiles();
			for (int f = 0; f < files.length; f++) {
				if (files[f].isFile()) {
					addDir(files[f], filenames, x, folderNode, "");
				} else if (files[f].isDirectory()) {
					filenames.add(files[f].toString());
					addDir(files[f], filenames, x + 1, folderNode, files[f]
							.toString());
				}
			}
		} else {
			if (file.isFile()) {
				DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(
						new TreeElement(file.getName(), file));
				model.insertNodeInto(childNode, rootNode, rootNode
						.getChildCount());
			}
		}

		// Show all the items added:
		this.applet.getJItemsTree().scrollPathToVisible(
				new TreePath(rootNode.getLastLeaf().getPath()));

	}
}